﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MakeIT.MiraPark.Negocios;

namespace MiraPark
{
    public partial class FormCadastroCabine : Form
    {
        private string operacao = "";

        public FormCadastroCabine()
        {
            InitializeComponent();
        }

        private void FormCadastroCabine_Load(object sender, EventArgs e)
        {
            //Popular a lista de cabines cadastrados
            this.dataGridViewCabines.DataSource = ModelBO.getCabines();
            this.comboBoxPatio.DataSource = ModelBO.getPatios();

            setupButtonsControls();
        }

        private void dataGridViewCabine_SelectionChanged(object sender, EventArgs e)
        {
            setupButtonsControls();
        }

        private void buttonNovo_Click(object sender, EventArgs e)
        {
            this.operacao = "novo";
            setupButtonsControls();
        }

        private void buttonCancelar_Click(object sender, EventArgs e)
        {
            this.operacao = "cancelar";
            setupButtonsControls();
        }

        private void buttonEditar_Click(object sender, EventArgs e)
        {
            this.operacao = "editar";
            setupButtonsControls();
        }

        private void buttonExcluir_Click(object sender, EventArgs e)
        {
            List<CabineVO> cabineList = (List<CabineVO>)this.dataGridViewCabines.DataSource;
            CabineVO cabine = cabineList[this.dataGridViewCabines.CurrentRow.Index];

            this.operacao = "excluir";

            DialogResult dr = MessageBox.Show(this, "Confirma a exclusão da cabine " + cabine.cabine, "Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr.ToString().Equals("Yes"))
            {
                ModelBO.delCabine(cabine);

                int nRow = this.dataGridViewCabines.CurrentRow.Index;
                this.dataGridViewCabines.DataSource = ModelBO.getCabines();
                if (this.dataGridViewCabines.Rows.Count > 0)
                {
                    dataGridViewCabines.CurrentCell = dataGridViewCabines.Rows[Math.Min(nRow, this.dataGridViewCabines.Rows.Count - 1)].Cells[1];
                    dataGridViewCabines.Rows[Math.Min(nRow, this.dataGridViewCabines.Rows.Count - 1)].Selected = true;
                }

                setupButtonsControls();
            }
        }

        private void buttonSalvar_Click(object sender, EventArgs e)
        {
            String erros = "";
            if (this.textBoxCabine.Text.Trim().Length == 0)
                erros = erros + "Cabine deve ser informada.\r\n";
            if (this.comboBoxPatio.SelectedValue == null)
                erros = erros + "Pátio deve ser informado.\r\n";
            
            if (erros.Length > 0)
            {
                MessageBox.Show(this, erros, "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            CabineVO cabine = null;

            if (this.operacao.Equals("novo"))
            {

                cabine = CabineVO.CreateCabineVO();
                cabine.cabine = this.textBoxCabine.Text;
                cabine.idPatio = (int)this.comboBoxPatio.SelectedValue;

                cabine.idCabine = (int)ModelBO.newCabine(cabine);
            }
            if (this.operacao.Equals("editar"))
            {
                List<CabineVO> cabineList = (List<CabineVO>)this.dataGridViewCabines.DataSource;
                cabine = cabineList[this.dataGridViewCabines.CurrentRow.Index];

                cabine.cabine = this.textBoxCabine.Text;
                cabine.idPatio = (int)this.comboBoxPatio.SelectedValue;

                ModelBO.setCabine(cabine);
            }
            this.operacao = "salvar";

            List<CabineVO> listCabineVOs = ModelBO.getCabines();
            dataGridViewCabines.DataSource = listCabineVOs;
            int nRow = listCabineVOs.FindIndex(a => a.idCabine == cabine.idCabine);
            dataGridViewCabines.CurrentCell = dataGridViewCabines.Rows[nRow].Cells[1];
            dataGridViewCabines.Rows[nRow].Selected = true;

            setupButtonsControls();
        }


        private void setupButtonsControls()
        {
            bool haRows = this.dataGridViewCabines.Rows.Count > 0 && this.dataGridViewCabines.CurrentRow!=null;
            bool novo = this.operacao.Equals("novo");
            bool edit = this.operacao.Equals("editar");
            bool canc = this.operacao.Equals("cancelar");

            this.buttonCancelar.Enabled = novo || edit;
            this.buttonNovo.Enabled = !novo && !edit;
            this.buttonEditar.Enabled = haRows && !novo && !edit;
            this.buttonExcluir.Enabled = haRows && !novo && !edit;
            this.buttonSalvar.Enabled = novo || edit;

            this.textBoxCabine.Enabled = novo || edit;
            this.comboBoxPatio.Enabled = novo || edit;

            this.dataGridViewCabines.Enabled = !novo && !edit;

            if (!haRows || novo)
            {
                this.textBoxId.Text = "";
                this.textBoxCabine.Text = "";
            }
            else {
                List<CabineVO> cabineList = (List<CabineVO>)this.dataGridViewCabines.DataSource;
                CabineVO cabine = cabineList[this.dataGridViewCabines.CurrentRow.Index];
                this.textBoxId.Text = cabine.idCabine.ToString();
                this.textBoxCabine.Text = cabine.cabine;
                this.comboBoxPatio.SelectedValue = cabine.idPatio;
            }
            if (novo || edit){
                this.textBoxCabine.Focus();
            }
        }

        private void buttonListagem_Click(object sender, EventArgs e)
        {
            FormReportCabine formx = new FormReportCabine();
            formx.MdiParent = this.MdiParent;
            formx.Show();
            

        }

     }
}
